using System;
using UnityEngine;
using QFramework;
using TMPro;

namespace Survivor
{
	public partial class FloatingTextController : ViewController
	{

		private static FloatingTextController _default;

		private void Awake()
		{
			_default = this;
		}
		
		private void OnDestroy()
		{
			_default = null;
		}


		public static void Play(Vector2 position, string text,Vector2 floatingDirection,bool isCritical = false)
		{
			_default.FloatingText.InstantiateWithParent(_default.transform).Self(self =>
			{
				var uiText = self.GetComponent<TextMeshProUGUI>();
				if (uiText)
				{
					uiText.text = text;
					uiText.transform.position = position;
					var initPosition = position;
					if (isCritical)
					{
						uiText.color = Color.red;
						uiText.transform.LocalScale(1.5f);
					}
					ActionKit.Sequence()
						.Lerp(0, 1f, 0.5f, (p) =>
						{
							uiText.transform.position = initPosition + p*floatingDirection;
						})
						.Delay(0.5f)
						.Lerp(1f,0,0.3f, (p) =>
						{
							uiText.ColorAlpha(p);
						})
						.Start(uiText, () => { self.DestroySelfGracefully();});
				}
			}).Show();
		}
	}
}
